package cn.lanqiao.blog.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



import cn.lanqiao.blog.entity.User;
import cn.lanqiao.blog.service.UserService;
import cn.lanqiao.blog.utils.FileUploadProperteis;
import cn.lanqiao.blog.utils.JwtUtils;
import cn.lanqiao.blog.utils.Result;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private JwtUtils jwtUtils;
	@Autowired
	private FileUploadProperteis upload;
	private Result R;
	
	
	//关注
	@RequestMapping("addAttention")
	public Result addAttention(Integer userId, Integer fansId) {
		if(userService.addAttention(userId, fansId) > 0) {
			return Result.success(200, "关注成功", 1);
		}else {
			return Result.fail("关注失败");
		}
	}
	
	//取关
	@RequestMapping("deleteAttention")
	public Result deleteAttention(Integer userId, Integer fansId) {
		if(userService.deleteAttention(userId, fansId) > 0) {
			return Result.success(200, "取关成功", 1); 
		}else {
			return Result.fail("取关失败");
		}
	}
	
	//拉黑
	@RequestMapping("addBlack")
	public Result addBlack(Integer userId, Integer blackId) {
		if(userService.addBlack(userId, blackId) > 0) {
			return Result.success(200, "拉黑成功", 1);
		}else {
			return Result.fail("拉黑失败");
		}
	}
	
	//取消拉黑
	@RequestMapping("deleteBlack")
	public Result deleteBlack(Integer userId, Integer blackId) {
		if(userService.deleteBlack(userId, blackId) > 0) {
			return Result.success(200, "移除黑名单成功", 1);
		}else {
			return Result.fail("移除失败");
		}
	}
	
	//粉丝列表
	@RequestMapping("getFansList")
	public Result getFansList(Integer userId, Integer page, Integer limit){
		int total = userService.getFansCount(userId);
		return Result.success(userService.getFansList(userId, page, limit), total);
	}
	
	//关注列表
	@RequestMapping("getAttentionList")
	public Result getAttentionList(Integer userId, Integer page, Integer limit){
		int total = userService.getAttentionCount(userId);
		return Result.success(userService.getAttentionList(userId, page, limit), total);
	}
	
	//粉丝数
	@RequestMapping("getFansCount")
	public int getFansCount(Integer userId) {
		return userService.getFansCount(userId);
	}
	
	//关注数
	@RequestMapping("getAttentionCount")
	public int getAttention(Integer userId) {
		return userService.getAttentionCount(userId);
	}
	
	//收藏夹文章数量
	@RequestMapping("getCollectionCount")
	public int getCollectionCount(Integer userId) {
		return userService.getCollectionCount(userId);
	}
	
	//排名
	@RequestMapping("getOrder")
	public Result getOrder(Integer userId) {
		if(userService.getOrder(userId) <= 10) {
			return Result.success(userService.getOrder(userId));
		}else if(userService.getOrder(userId) > 10 && userService.getOrder(userId) <= 50) {
			return Result.success("前50");
		}else if(userService.getOrder(userId) > 50 && userService.getOrder(userId) <= 100) {
			return Result.success("前100");
		}else {
			return Result.success("无");
		}
		
	}
	
	//排名列表
	@RequestMapping("getOrderList")
	public Result getOrderList(){
		return Result.success(userService.getOrderList());
	}
	
	//登录
		@RequestMapping("login")
		public Result login(String username,String password,HttpServletResponse response) {
			
			User user  = userService.login(username, password);
			if(user==null) {
				return R.fail("用户不存在或密码错误");
			}else {
				//判断是否被封禁
				if(user.getBan() == 0) {
					String jwt = jwtUtils.generateToken(user.getId());
				    response.setHeader("Authorization", jwt);
				    response.setHeader("Access-Control-Expose-Headers", "Authorization");
					return R.success(user);
				}
				return R.fail("您的账号已被封禁");
			}
		}
		
		//黑名单数
		@RequestMapping("getBlackCount")
		public Result getBlackCount(Integer userId) {
			return Result.success(userService.getBlackCount(userId));
		}
		
		//获赞数
		@RequestMapping("getPraiseCount")
		public Result getPraiseCount(Integer userId) {
			return Result.success(userService.getPraiseCount(userId));
		}
		
		//访客数
		@RequestMapping("getViewerCount")
		public Result getViewerCount(Integer userId) {
			return Result.success(userService.getViewerCount(userId));
		}
		
		//黑名单数
		@RequestMapping("getBlackList")
		public Result getBlackList(Integer userId, Integer page, Integer limit) {
			int total = userService .getBlackCount(userId);
			return Result.success(userService.getBlackList(userId, page, limit), total);
		}
		
		//访客列表
		@RequestMapping("getViewerList")
		public Result getViewerList(Integer userId, Integer page, Integer limit) {
			int total = userService .getViewerCount(userId);
			return Result.success(userService.getViewerList(userId, page, limit), total);
		}
		
		//是否收藏该文章
		@RequestMapping("isCollection")
		public Result isCollection(Integer userId, Integer articleId) {
			return Result.success(userService.isCollection(userId, articleId));
		}
		
		//是否点赞该文章
		@RequestMapping("isPraise")
		public Result isPraise(Integer userId, Integer articleId) {
			return Result.success(userService.isPraise(userId, articleId));
		}
		
		//用户修改头像
		@PostMapping("/upload")
		public Result upload(MultipartFile headImg) {
			try {
				String fileName = headImg.getOriginalFilename();
				String str = fileName.substring(fileName.lastIndexOf("."));
				String fileNewName = UUID.randomUUID().toString() + str;
				File newFile = new File(upload.getUploadFolder() + "/" + fileNewName);
				if(!newFile.exists()) {
				newFile.mkdirs();
				newFile.createNewFile();
				}
				headImg.transferTo(newFile);
				if(fileNewName != null && !fileNewName.equals("")) {
					return Result.success(200, "头像上传成功", "http://localhost:1109/head/" + fileNewName);
				}else {
					return Result.fail("头像上传失败");
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return Result.fail("头像上传失败");
	}
		
		//用户修改信息
		@RequestMapping("save")
		public Result saveUser(User user) {
			int r = userService.updateUser(user);
			if(r>0) {
				return Result.success(user);
			}else {
				return Result.fail("修改失败");
			}
		}
		
		//访客增加
		@RequestMapping("addViewer")
		public Result addViewer(Integer adminId, Integer viewerId) {
			return Result.success(userService.addViewer(adminId, viewerId));
		}
		
		//获取用户信息
		@RequestMapping("getUserInfo")
		public Result getUserInfo(Integer userId) {
			return Result.success(userService.getUserInfo(userId));
		}
		
		//判断是否拉黑
		@RequestMapping("isBlack")
		public Result isBlack(Integer viewerId, Integer adminId) {
			return Result.success(userService.isBlack(viewerId, adminId));
		}
		
		//判断是否关注
		@RequestMapping("isAttention")
		public Result isAttention(Integer viewerId, Integer adminId) {
			return Result.success(userService.isAttention(viewerId, adminId));
		}
		
		@RequestMapping("/getpage2")
		public Result getUserPage2(@RequestParam("page")Integer pageNum,@RequestParam("limit")Integer pageSize,User user){
			return Result.success(userService.getUserPage2(pageNum, pageSize, user));	
		}
		
		@RequestMapping("/insert")
		public Result insert(User user) {
			return Result.success(userService.insertUser(user));
		}
		
		@RequestMapping("/delete")
		public Result delete(Integer id) {
			return Result.success(userService.deleteUser(id));
		}
		
		@RequestMapping("/ban")
		public Result ban(User user) {
			return Result.success(userService.banUser(user));
		}
		
		@RequestMapping("/adminLogin")
		public Result adminLogin(String username, String password) {
			User user = new User();
			user = userService.login(username, password);
			 if(user.getType() != 0 ) {
			 return Result.success(1);
			 }
			 return Result.success(0);
		}
		//注册
		@PostMapping("register")
		public Result register(User user) {
			int r = userService.register(user);
			if(r > 0) {
				return Result.success(200,"注册成功",user);
			}
			return Result.fail("注册失败");
		}
}
